<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <title>图书馆借阅系统 - 注册</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
  <style>
    body {
      background-color: #f0f8ff;
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      color: #212529;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
      margin: 0;
      padding: 0;
    }
    .auth-container {
      width: 100%;
      max-width: 500px;
      background-color: white;
      border-radius: 15px;
      overflow: hidden;
      box-shadow: 0 5px 15px rgba(0,0,0,0.1);
      padding: 40px;
    }
    .auth-title {
      font-size: 2rem;
      font-weight: bold;
      margin-bottom: 10px;
    }
    .auth-subtitle {
      font-size: 1rem;
      color: #777;
      margin-bottom: 40px;
    }
    .form-control {
      border-radius: 30px;
      padding: 15px 20px;
      margin-bottom: 20px;
      border: 1px solid #ddd;
      font-size: 1rem;
    }
    .form-control:focus {
      border-color: #4a90e2;
      box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.25);
    }
    .btn-register {
      background-color: #4a90e2;
      color: white;
      border: none;
      border-radius: 30px;
      padding: 15px 0;
      width: 100%;
      font-size: 1rem;
      font-weight: 500;
      cursor: pointer;
      transition: background-color 0.3s;
    }
    .btn-register:hover {
      background-color: #3a7bc8;
    }
    .back-to-login {
      text-align: center;
      margin-top: 20px;
      color: #777;
      font-size: 0.9rem;
    }
    .back-to-login a {
      color: #4a90e2;
      text-decoration: none;
    }
  </style>
  <meta charset="utf-8">
</head>
<body>
<div class="auth-container">
  <div class="auth-title">WELCOME! 图书馆借阅系统</div>
  <div class="auth-subtitle">创建你的账户，开始借阅之旅</div>

  <form id="register-form">
    <div class="input-group">
      <input type="text" id="username" class="form-control" placeholder="用户名" required>
    </div>
    <div class="input-group">
      <input type="password" id="password" class="form-control" placeholder="密码" required>
    </div>
    <div class="input-group">
      <input type="password" id="confirmPassword" class="form-control" placeholder="确认密码" required>
    </div>
    <button type="submit" class="btn btn-register">注册</button>
  </form>
  <div class="back-to-login">
    <a href="login.html">已有账户？前往登录</a>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
  document.getElementById('register-form').addEventListener('submit', function(e) {
    e.preventDefault();

    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;
    const confirmPassword = document.getElementById('confirmPassword').value;

    if (password !== confirmPassword) {
      alert('两次输入的密码不一致，请重新输入。');
      return;
    }

    // 发送POST请求到后端注册API
    fetch('/api/auth/register', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        username: username,
        password: password
      })
    })
            .then(response => response.json())
            .then(data => {
              if (data.code === 200) {
                alert('注册成功！');
                window.location.href = 'login.html'; // 注册成功后跳转到登录页面
              } else {
                alert('注册失败，请稍后再试。');
              }
            })
            .catch(error => {
              console.error('注册请求出错:', error);
              alert('注册请求出错，请稍后再试。');
            });
  });
</script>
</body>
</html>